<?php

namespace App\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use Psr\Log\LoggerInterface;

class BaseController extends FOSRestController
{
    protected $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * 返回成功数据
     * @param array  $data    需要返回的数据
     * @param string $_format
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    final public function successView($data = [], $_format)
    {
        $view = $this->view(['code' => 200, 'message' => 'ok', 'data' => $data])->setFormat($_format);

        return $this->handleView($view);
    }

    /**
     * 返回失败数据.
     *
     * @param integer $code    code码
     * @param string  $message 报错信息
     * @param array   $data    需要返回的数据
     * @param string  $_format
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    final public function errorView($code, $message, $data = [], $_format = 'json')
    {
        $view = $this->view(['code' => $code, 'message' => $message, 'data' => $data,])->setFormat($_format);

        return $this->handleView($view);
    }
}
